home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1,comp.lang.apl
- Subject: Re: GOTO controversy
- Date: 21 Mar 1996 16:16:58 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4isrhqINNpol@keats.ugrad.cs.ubc.ca>
- References: <314FB5F5.259B@simi.is> <3151B47F.70FD@connix.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <3151B47F.70FD@connix.com>,
- Scott Hawley <shawley@connix.com> wrote:
- >What to you think? Loops without using loops?
- >
- >a loop:
- >
- >for(i=0;i<10;i++)
- >{
- > stuff
- >}
- >
- >Looping with out a loop
- >
- >no_loop(0,10);
- >
- >no_loop(int start, int end)
- >{
- > if(start != end) {
- > stuff
- > no_loop(start+1,end);
-
- recursion! ouch! I'd hate to do a no_loop(1,1000000000);
-
- > }
- >}
- >
- >I haven't tested this but you get the idea.
-
- How do you intend to pass ``stuff'' to the no_loop function? I think you are a
- little confused about C! :)
-
- For this to be of any practical use, one should be able to supply an arbitrary
- statement block in the place of stuff. One way to do this is to define no_loop
- as a pre-processor macro. This has been done since the dawn of C for creating
- idioms for traversing linked list and other data structures. I have recently
- seen a program written in 1980 which contains an example.
-
-
- #define no_loop(S,E) for (;(S) < (E);(S)++)
-
- You then do:
-
- int x, y = 4;
-
- no_loop(x, y) {
- printf("%i\n",x);
- }
-
- The first argument to no_loop must be an lvalue, and it gets incremented, so
- this does not have the semantics you were looking for in your own definition of
- the no_loop() function.
-
- How this is better than using an explicit for() loop is not clear to me. Such a
- macro is useful when you have to traverse some complex data structure. In my
- own hash-table module, I define a macro called 'hash_for', which will step
- through all the elements inserted into the hash table and set the specified
- node pointer to each one in turn. The reason I have this is because the hash
- table is somewhat involved. There have to be two nested loops to do the
- traversal. The outer one steps through the array of chains, and the inner one
- traverses any non-empty chain. Written explicitly, the code looks ugly and
- would confound any program that has to many such traversals in various places.
-
- Instead, all I have to do is:
-
- hash_for(hash_table_ptr, data_ptr) {
- /* process data_ptr */
- } hash_rof;
-
-
- --
-
-